package com.muki.succ.registery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.muki.spider.Spider;
import com.muki.spider.SpiderRegisteration;
import com.muki.succ.uri.MukiUriAcceptor;
import com.muki.succ.uri.UriCollector;


/**
 * 注册中心
 * @author ljh_2017
 *
 */
public class RegisteryCenter implements SpiderRegisteration,UriRegisteration {

	private static final Logger log = Logger.getLogger(RegisteryCenter.class);
	private static final SpiderManagerHall hall = SpiderManagerHall.newInstance();

	//等待队列
	//public static Queue<Spider> wait_spiders = new LinkedList<Spider>();
	//运行队列
	//public static Queue<Spider> running_spiders = new LinkedList<Spider>();

	public static Map<String, Spider> spiders = new HashMap<>();

	//单例模式
	private static final RegisteryCenter center = new RegisteryCenter();	

	//private final List<String> topics = new ArrayList<>();
	
	public static RegisteryCenter getInstance() {
		return center;
	}

	@Override
	public void createSpiderTopicListener(String topic) {
		//接收一个topic消息
		//去SpiderManager注册一个爬虫到该topic下
		hall.findByName(topic).sendSpiderToTopic(this);
		//该爬虫下需要一个接收者，负责接收uri
		UriCollector coll = new MukiUriAcceptor();
		//设置消息监听口
		//该监听口一直负责监听消息队列口，获取消息
		coll.setListenerEntering(topic);
		
		//topics.add(topic);
	}

	@Override
	public void Response() {
		log.info("正在接收爬虫");
	}

	@Override
	public void register(Spider spider) {
		Response();
		log.info("正在注册爬虫:"+spider.getName()+",爬虫任务主题为："+spider.getTopic());
		spiders.put(spider.getName(), spider);
	}

}
